﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.EShop.Bll;
using Runda.EShop.Common.Entity;
using System.Text;

namespace Runda.EShop.Web.UserControl
{
    public partial class LeftMenu : UserControlBase
    {
        /// <summary>
        /// 获取或设置待显示菜单的顶层菜单代码
        /// </summary>
        public string TopMenuCode
        {
            get
            {
                return hidMenuCode.Value;
            }
            set
            {
                hidMenuCode .Value = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            { 
            }
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<div class=\"i_left_nov\">\r\n");
            writer.Write(BuildMenuToHTML(TopMenuCode));
            writer.Write("</div>");
        }

        private string BuildMenuToHTML(string topMenuCode)
        {
            if (Page == null) return "";
            SystemBll bll = new SystemBll();
            List<SysPageInfoEntity> list = bll.LoadAllPageInfo();

            List<SysPageInfoEntity> listSub = list.FindAll(item => item.ParentPageID == topMenuCode && item.IsMenu == 1); 

            StringBuilder sb = new StringBuilder();
            if (listSub != null)
            {
                foreach (SysPageInfoEntity second in listSub)
                {
                    sb.Append("<h2><img src=\"").Append(ResolveUrl("~/images/ico/document-excel-table.gif")).Append("\" width=\"16\" height=\"16\" style=\"vertical-align: middle\" />&nbsp;").Append(second.PageName).Append("</h2>");

                    List<SysPageInfoEntity> child = list.FindAll(item => item.ParentPageID == second.PageID && item.IsMenu == 1);
                    if (child != null && child.Count > 0)
                    {
                        sb.AppendLine("<ul>");
                        foreach (SysPageInfoEntity fun in child)
                        {
                            if (  fun.URL == base.Page.CurrentAppPath)
                            {
                                sb.Append("<li class=\"i_left_nov1\">");
                            }
                            else
                            {
                                sb.Append("<li>");
                            }
                            sb.Append("<a href=\"").Append(ResolveUrl(fun.URL)).Append("\">").Append(fun.PageName).AppendLine("</a></li>");
                        }
                        sb.AppendLine("</ul>");
                    }
                }
                return sb.ToString();
            }
            return string.Empty;
        }
    }
}
